﻿Imports System.Windows.Threading
Imports System.Collections.ObjectModel
Imports System.Threading.Tasks
Imports System.Threading

Namespace Statistics
    Public Class ArticleStats

        Private _dispatcher As Dispatcher
        Private _loader As IDatabase
        Private _usr As User

        Public Sub New(dp As Dispatcher, loader As IDatabase, usr As User)
            _dispatcher = dp
            _loader = loader
            _usr = usr
        End Sub

        ''' <summary>
        ''' Gets the most bought articles in the given time intervall. Calls the "GetMostBoughtArticlesCompleted"-Event
        ''' </summary>
        ''' <param name="startdate"></param>
        ''' <param name="enddate"></param>
        ''' <remarks></remarks>
        Public Sub GetMostBoughtArticlesAsync(startdate As Date, enddate As Date)
            Task.Factory.StartNew(Sub()
                                      GetMostBoughtArticles(New List(Of String), startdate, enddate)
                                  End Sub)
        End Sub

        ''' <summary>
        ''' Calculates the most bought articles in the filter. Calls the "GetMostBoughtArticlesCompleted"-Event
        ''' </summary>
        ''' <param name="articles">The articles you want values for</param>
        ''' <param name="startdate"></param>
        ''' <param name="enddate"></param>
        ''' <remarks></remarks>
        Public Sub GetMostBoughhtArticlesAsync(articles As List(Of String), startdate As Date, enddate As Date)
            Task.Factory.StartNew(Sub()
                                      GetMostBoughtArticles(articles, startdate, enddate)
                                  End Sub)
        End Sub

        Public Sub GetMostBoughtArticles(filter As List(Of String), startdate As Date, enddate As Date)
            Dim output As New List(Of ArticleAmountModel)

            '### Filter relevant orders ###'
            Dim orders As Collection(Of OrderPreviewModel) = _loader.GetOrdersInInterval(startdate, enddate, _usr)
            'Dim orderstodel As New Collection(Of Order)
            'For Each o In orders
            '    If Not CompanyStats.IsDateBetween(startdate, enddate, o.Datum.ToDate) Then
            '        orderstodel.Add(o)
            '    End If
            'Next
            'DeleteItemsFromCollection(orders, orderstodel)
            'orderstodel.Clear()

            '### Calcualte ###'
            For Each o In orders
                For Each a In _loader.GetOrder(o.OrderID, o.OrderTyp, _usr).OrderedArticles
                    If filter.Contains(a.ArticleID) OrElse filter.Count = 0 Then
                        Dim item As New ArticleAmountModel(a, a.OrderedAmount, a.GrossOrderValue)
                        If output.Contains(item) Then
                            item = output.Item(output.IndexOf(item))
                            item.Amount += a.OrderedAmount
                            item.Proceeds += a.GrossOrderValue
                        Else
                            output.Add(item)
                        End If
                    End If
                Next
            Next

            Dim sort As New ArticleAmountProceedsSort
            output.Sort(sort)

            Dim result As New Collection(Of ArticleAmountModel)
            For Each item In output
                result.Add(item)
            Next
            _dispatcher.BeginInvoke(Sub()
                                        RaiseEvent GetMostBoughtArticlesCompleted(result)
                                    End Sub)
        End Sub

        Public Event GetMostBoughtArticlesCompleted(result As Collection(Of ArticleAmountModel))

        Private Sub DeleteItemsFromCollection(ByRef basecollection As Collection(Of Order), ItemsToDelete As Collection(Of Order))
            For Each item In ItemsToDelete
                basecollection.Remove(item)
            Next
        End Sub
    End Class
End Namespace

